package br.ufmg.dees.insane.ui.rich.prep.view.dialog;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;

import br.ufmg.dees.insane.ui.rich.prep.view.InterfacePrep;

public class CopyFrameDialog extends JDialog implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JPanel paneMain = null;
	private JPanel paneRadioBtn = null;
	private JPanel panelImput = null;
	private JPanel panelButtons = null;

	private JRadioButton rbtnAxisX = null;
	private JRadioButton rbtnAxisY = null;
	private JRadioButton rbtnAxisZ = null;

	private JTextField txtfNumCopies = null;
	private JTextField txtfDistance = null;

	private JButton btnOk = null;
	private JButton btnCancel = null;
	
	private JLabel lblNumCopy = null;
	private JLabel lblDistance = null;
	
	private int ncopy;
	private String axisDir;
	private double dist;
	
	private InterfacePrep intFrame;


//  ****************************************************************************
	/**
	 * @param owner
	 */
	public CopyFrameDialog(InterfacePrep parent)
	{
		this.intFrame = parent;
		initialize();
	}

	/**
	 * This method initializes this
	 * @return void
	 */
	private void initialize() {
		this.setTitle("Copy Frame");
		this.setResizable(false);
		this.setModal(true);
		this.setPreferredSize(new Dimension(200, 200));
		this.setBounds(new Rectangle(0, 0, 200, 200));
		this.setName("dlgCopias");
		this.setMinimumSize(new Dimension(200, 180));
		this.setMaximumSize(new Dimension(200, 180));
		this.setContentPane(getPaneMain());
		this.setLocationRelativeTo(intFrame);
		this.validate();
		this.setVisible(true);
	}

	/**
	 * This method initializes paneMain
	 * @return javax.swing.JPanel
	 */
	private JPanel getPaneMain() {
		if (paneMain == null) {
			paneMain = new JPanel();
			paneMain.setLayout(new BoxLayout(getPaneMain(), BoxLayout.Y_AXIS));
			paneMain.setPreferredSize(new Dimension(200, 200));
			paneMain.add(getPaneRadioBtn(), null);
			paneMain.add(getPanelImput(), null);
			paneMain.add(getPanelButtons(), null);
		}
		return paneMain;
	}

	/**
	 * This method initializes paneRadioBtn	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPaneRadioBtn() {
		if (paneRadioBtn == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setHgap(25);
			flowLayout.setVgap(0);
			paneRadioBtn = new JPanel();
			paneRadioBtn.setPreferredSize(new Dimension(150, 30));
			paneRadioBtn.setLayout(flowLayout);
			paneRadioBtn.setBorder(BorderFactory.createTitledBorder(null, "Axis Direction", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
	        ButtonGroup btn_grupo = new ButtonGroup();
            btn_grupo.add(getRbtnAxisX());
            btn_grupo.add(getRbtnAxisY());
            btn_grupo.add(getRbtnAxisZ());
			paneRadioBtn.add(getRbtnAxisX(), null);
			paneRadioBtn.add(getRbtnAxisY(), null);
			paneRadioBtn.add(getRbtnAxisZ(), null);
		}
		return paneRadioBtn;
	}

	/**
	 * This method initializes rbtnAxisX	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getRbtnAxisX() {
		if (rbtnAxisX == null) {
			rbtnAxisX = new JRadioButton();
			rbtnAxisX.setText("X");
			rbtnAxisX.addActionListener(this);
		}
		return rbtnAxisX;
	}

	/**
	 * This method initializes rbtnAxisY	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getRbtnAxisY() {
		if (rbtnAxisY == null) {
			rbtnAxisY = new JRadioButton();
			rbtnAxisY.setText("Y");
			rbtnAxisY.addActionListener(this);
		}
		return rbtnAxisY;
	}

	/**
	 * This method initializes rbtnAxisZ	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getRbtnAxisZ() {
		if (rbtnAxisZ == null) {
			rbtnAxisZ = new JRadioButton();
			rbtnAxisZ.setText("Z");
			rbtnAxisZ.addActionListener(this);
			rbtnAxisZ.setSelected(true);
			axisDir = "Z";
		}
		return rbtnAxisZ;
	}

	/**
	 * This method initializes panelImput	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPanelImput() {
		if (panelImput == null) {
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(2);
			gridLayout.setHgap(5);
			gridLayout.setVgap(5);
			gridLayout.setColumns(2);
			lblDistance = new JLabel();
			lblDistance.setText("Distance (+/-)");
			lblDistance.setHorizontalAlignment(SwingConstants.LEFT);
			lblDistance.setHorizontalTextPosition(SwingConstants.LEFT);
			lblDistance.setPreferredSize(new Dimension(80, 20));
			lblNumCopy = new JLabel();
			lblNumCopy.setText("Num of Copies");
			lblNumCopy.setPreferredSize(new Dimension(80, 20));
			lblNumCopy.setHorizontalAlignment(SwingConstants.LEFT);
			lblNumCopy.setHorizontalTextPosition(SwingConstants.LEFT);
			lblNumCopy.setVisible(true);
			panelImput = new JPanel();
			panelImput.setLayout(gridLayout);
			panelImput.setPreferredSize(new Dimension(150, 55));
			panelImput.setBorder(BorderFactory.createTitledBorder(null, "Features", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			panelImput.setForeground(Color.black);
			panelImput.add(lblNumCopy, null);
			panelImput.add(getTxtfNumCopies(), null);
			panelImput.add(lblDistance, null);
			panelImput.add(getTxtfDistance(), null);
		}
		return panelImput;
	}

	/**
	 * This method initializes txtfNumCopies	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtfNumCopies() {
		if (txtfNumCopies == null) {
			txtfNumCopies = new JTextField();
			txtfNumCopies.setPreferredSize(new Dimension(80, 20));
			txtfNumCopies.setText("1");
		}
		return txtfNumCopies;
	}

	/**
	 * This method initializes panelButtons	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPanelButtons() {
		if (panelButtons == null) {
			FlowLayout flowLayout1 = new FlowLayout();
			flowLayout1.setHgap(10);
			flowLayout1.setVgap(5);
			panelButtons = new JPanel();
			panelButtons.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
			panelButtons.setPreferredSize(new Dimension(150, 15));
			panelButtons.setLayout(flowLayout1);
			panelButtons.add(getBtnOk(), null);
			panelButtons.add(getBtnCancel(), null);
		}
		return panelButtons;
	}

	/**
	 * This method initializes btnOk	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnOk() {
		if (btnOk == null) {
			btnOk = new JButton();
			btnOk.setPreferredSize(new Dimension(80, 25));
			btnOk.setText("Ok");
			btnOk.setText("Ok");
			btnOk.setPreferredSize(new Dimension(75, 25));
			btnOk.addActionListener(this);
	        getRootPane().setDefaultButton(btnOk);
		}
		return btnOk;
	}

	/**
	 * This method initializes btnCancel	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new JButton();
			btnCancel.setText("Cancel");
			btnCancel.setPreferredSize(new Dimension(80, 25));
			btnCancel.addActionListener(this);

		}
		return btnCancel;
	}

	/**
	 * This method initializes txtfDistance	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtfDistance() {
		if (txtfDistance == null) {
			txtfDistance = new JTextField();
			txtfDistance.setText("-3.0");
			txtfDistance.setPreferredSize(new Dimension(80, 20));
		}
		return txtfDistance;
	}
	
//  ****************************************************************************	
    
	public void actionPerformed(ActionEvent e) {
        
        if(e.getSource() == btnOk)
        {
    		try 
    		{
    			ncopy = Integer.valueOf(this.txtfNumCopies.getText());
    			dist = Double.valueOf(this.txtfDistance.getText());
    			this.setVisible(false);
    		}
    		catch (Exception exc) 
    		{
    			String msg = "Some value (int or double) is not valid. Verify !";
    			JOptionPane.showMessageDialog(this, msg,
    					"Alert Message", JOptionPane.INFORMATION_MESSAGE);
    			txtfNumCopies.requestFocus();
    		}
        }
        else if (e.getSource() == rbtnAxisX)
        {
        	this.axisDir = "X";
        }
        else if (e.getSource() == rbtnAxisY)
        {
        	this.axisDir = "Y";
        }
        else if (e.getSource() == rbtnAxisZ)
        {
        	this.axisDir = "Z";
        }
        else
        {
        	this.setVisible(false);
        }
    }
	
//  ****************************************************************************	
	
	public int getNumCopies()
	{
		return this.ncopy;
	}
	
	public double getDistance()
	{
		return this.dist;
	}
	
	public String getAxisDirecion()
	{
		return this.axisDir;
	}	
	
//  ****************************************************************************
	
//    public static void main(String[]args)
//    { CopyEdgesDialog app = new CopyEdgesDialog(null); }
    
//  ****************************************************************************
}// end of this class
